﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using System.Text;
using Com.Toook.ParseHtml;

/// <summary>
///Robots 的摘要说明
/// </summary>
public class Robots
{
	public Robots(string visitPage, string ip, string agent)
	{
        this.VisitCount = 1;//第一次初始化为1
        this.VisitCountIndex = 0;
        this.VisitPages[this.VisitCountIndex] = visitPage;
        this.VisitTimes[this.VisitCountIndex] = DateTime.Now;
        this.VisitIp = ip;
        this.VisitAgent = agent;        
	}   
    /// <summary>
    /// 访问页面数.第一次初始化为1
    /// </summary>
    public int VisitCount;
    /// <summary>
    /// 从0-9循环
    /// </summary>
    private int VisitCountIndex;
    public string VisitIp;
    public string VisitAgent;
    /// <summary>
    /// 判断是否是其他采集程序机器人,0:普通用户, 1:怀疑, 2:跳到验证页 3:禁止访问
    /// </summary>
    /// <param name="visitPage"></param>
    /// <returns>0:普通用户, 1:怀疑, 2:跳到验证页 3:禁止访问</returns>
    public int IsOtherRobot(string visitPage, string ip, string agent)
    {
        int robotLevel = 0;        
        if(VisitCount < 4 )//|| IsExistPage(visitPage) 1,2,3 前3个合法
        {
            robotLevel = 0;
        }
        else if(VisitCount < 7)//4,5,6 判断时间间隔是否小于2秒
        {
            TimeSpan ts = DateTime.Now - VisitTimes[0];
            if(ts.Hours == 0 && ts.Minutes == 0 && ts.Seconds <= 2)
            {                
                SaveResultToAccess.SaveRobotVisitRecord(this);//记录到数据库
                robotLevel = 1;
            }            
        }
        else if(VisitCount < 10)//7,8,9 判断时间间隔是否小于3秒，跳到验证页
        {
            TimeSpan ts = DateTime.Now - VisitTimes[0];
            if(ts.Hours == 0 && ts.Minutes == 0 && ts.Seconds <= 4)
            {
                SaveResultToAccess.SaveRobotVisitRecord(this);//记录到数据库
                robotLevel = 2;//跳到验证页
            }            
        }
        else//访问次数大于10次，还小于6秒，可以判断为机器人，禁止访问
        {
            TimeSpan ts = DateTime.Now - VisitTimes[0];
            if(ts.Hours == 0 && ts.Minutes == 0 && ts.Seconds <= 6)
            {
                SaveResultToAccess.SaveRobotVisitRecord(this);//记录到数据库
                robotLevel = 3;//禁止访问
            }
            //合法用户每20次记录一次
            if(this.VisitCount % 20 == 0)
            {
                //访问20页，小于60秒，机器人
                if(ts.Hours == 0 && ts.Minutes == 0 && ts.Seconds < 60)
                {
                    robotLevel = 2;//跳到验证页
                }
                SaveResultToAccess.SaveRobotVisitRecord(this);
            }
        }
        this.VisitCount++;//总访问数加1
        this.VisitCountIndex++;//索引加1
        if(this.VisitCountIndex == 10)
        {
            this.VisitCountIndex = 0;//重置为0
        }
        this.VisitPages[this.VisitCountIndex] = visitPage;
        this.VisitTimes[this.VisitCountIndex] = DateTime.Now;

        return robotLevel;
    }
    /// <summary>
    /// 判断页面是否已经存在。防止刷新的统计
    /// </summary>
    /// <param name="visitPage"></param>
    /// <returns></returns>
    private bool IsExistPage(string visitPage)
    {
        bool exist = false;
        for(int i = 0; i < VisitCount; i++)
        {
            if(VisitPages[i] == visitPage)
            {
                exist = true;
                break;
            }
        }
        return exist;
    }
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder(512);
        //sb.Append("Robot:ip=" + VisitIp + ", VisitCount=" + VisitCount.ToString() + ", Agent=" + VisitAgent);
        sb.Append("Robot:VisitCount=" + VisitCount.ToString());//ip,agent在保存时添加到单独字段
        for(int i = 0; i < 10 && i < VisitCount; i++)
        {
            sb.Append(", page=").Append(VisitPages[i]).Append(", time=").Append(VisitTimes[i].ToString("MM-dd HH:mm:ss.fff"));
        }
        return sb.ToString();
    }

    /// <summary>
    /// 第1个访问的页面
    /// </summary>
    public string[] VisitPages = new string[10];
    /// <summary>
    /// 访问第一个页面的时间
    /// </summary>
    public DateTime[] VisitTimes = new DateTime[10];
    
}
